package net.pm.core.web.material;

import javax.annotation.Resource;

import net.pm.common.toolkit.DevConstant;
import net.pm.common.toolkit.Pager;
import net.pm.core.annotation.MaterialLog;
import net.pm.core.annotation.PowerCode;
import net.pm.core.annotation.PowerCode.PowerCodeEnum;
import net.pm.core.aspect.PM_03_MateriallogAspect;
import net.pm.core.entities.db.Material;
import net.pm.core.entities.db.Materiallog;
import net.pm.core.entities.db.Pmuser;
import net.pm.core.service.MaterialServicer;
import net.pm.core.web.BaseController;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@Scope("request")
@RequestMapping("/material/")
public class MaterialController extends BaseController {
	
	/**
	 * 重定向到物料管理页面
	 */

	
	@Resource(name="MaterialServicer")
	private MaterialServicer materialServicer;
	
	/**
	 * 去到物理管理页面
	 * @return
	 */
	@RequestMapping(value="manage",method=RequestMethod.GET)
	public ModelAndView manage() {
		ModelAndView mav = new ModelAndView("material/material_manage");
		return mav;
	}
	
	/**
	 * 自动转到默认页
	 * @return
	 * */
	@RequestMapping(value = "page", method = RequestMethod.GET)
	public ModelAndView page() {
		return page(DevConstant.PAGE_DEFAULT_SIZE,DevConstant.PAGE_DEFAULT_NO,null);
	}
	
	/**
	 *  物理管理页面
	 * @return
	 */
	@RequestMapping(value="page",method=RequestMethod.POST)
	public ModelAndView page(Integer pagesize, Integer pageno,
			String materialName) {
		ModelAndView mav = new ModelAndView("material/material_page");
		Pager<Material> pager = materialServicer.findMaterialPager(pagesize,
				pageno, materialName);
		mav.addObject("pager", pager);
		return mav;
	}
	
	/**
	 * 去到物料添加页面
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.CREATE_MATERIAL)
	@RequestMapping(value="create",method=RequestMethod.GET)
	public String materialCreate(){
		return "material/material_create";
	}
	
	/**
	 * 新增物料入库
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.CREATE_MATERIAL)
	@MaterialLog
	@RequestMapping(value="create",method=RequestMethod.POST)
	public ModelAndView materialCreate(Material material) {
		ModelAndView mav = new ModelAndView("redirect:/material/create.htm");
		Long retval = materialServicer.createMaterial(material);
		if (retval.equals(MaterialServicer.create_material_success)) {
			super.setSessionMessage("新增物料成功!");
			{// 为作记录作准备
				Materiallog materiallog = new Materiallog(material.getInventoryNumber(), 
						new Material(material.getId()), 
						new Pmuser(super.obtainCurrentUser().getId()),
						Materiallog.status_put);
				mav.addObject(PM_03_MateriallogAspect.MATERIALLOG_KEY, materiallog);
			}
		} else {
			super.setSessionMessage("新增物料失败!");
		}
		return mav;
	}
	
	
	/**
	 * 查看物料详细信息(包括物料出入情况)
	 * @return
	 */
	@RequestMapping(value="info",method=RequestMethod.GET)
	public ModelAndView materialInfo(Long materialId) {
		ModelAndView mav = new ModelAndView("material/material_info");
		Material material = materialServicer.findMaterialById(materialId);
		mav.addObject("material", material);
		return mav;
	}
	
	
	
}
